//
//  LDPayViewController.m
//  dmao-customer
//
//  Created by macbook on 2017/10/17.
//  Copyright © 2017年 秦慕乔. All rights reserved.
//

#import "LDPayViewController.h"
#import "LDPushOrderViewController.h"

@interface LDPayViewController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *IconImg;
@property (weak, nonatomic) IBOutlet UILabel *StoreName;
@property (weak, nonatomic) IBOutlet UILabel *DateLabel;
@property (weak, nonatomic) IBOutlet UILabel *ProfitLabel;
@property (weak, nonatomic) IBOutlet UITextField *MoneyTF;
@property (weak, nonatomic) IBOutlet UIButton *SubmitBtn;
@property (assign, nonatomic) float per;
@end

@implementation LDPayViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.titleLabel.text = @"支付详情";
    _MoneyTF.delegate = self;
    _MoneyTF.keyboardType = UIKeyboardTypeDecimalPad;
    [self downloadData];
    [self judgeLoginEnabled];
}

-(void)judgeLoginEnabled{
    
    [_MoneyTF.rac_textSignal subscribeNext:^(NSNumber *result) {
        if(String(result).length > 0){
            [_SubmitBtn setBackgroundColor:[UIColor redColor]];
            _SubmitBtn.enabled = YES;
        }else{
            [_SubmitBtn setBackgroundColor:[UIColor grayColor]];
            _SubmitBtn.enabled = NO;
        }
        
    }];
}

- (void)downloadData
{
    [THWManager PostHttp:API(LDPay) Parameters:@{@"lineId":self.lineId, @"userToken":[Utils getUserToken]} Success:^(id responseObject) {
        [self.view removeAnyView];
        if ([responseObject[@"data"] isKindOfClass:[NSDictionary class]]) {
            if (![TXUtilsString isBlankString:[Utils getData:responseObject][@"shopImg"]]) {
                [_IconImg sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://%@", [Utils getData:responseObject][@"shopImg"]]] placeholderImage:[UIImage imageNamed:@"NoStore"] options:SDWebImageRetryFailed];
            }else{
                [_IconImg setImage:[UIImage imageNamed:@"NoStore"]];
            }
            _per = [[Utils getData:responseObject][@"per"][@"per"] floatValue];
            _StoreName.text = [NSString stringWithFormat:@"%@", [Utils getData:responseObject][@"shopName"]];
            _DateLabel.text = [NSString stringWithFormat:@"营业时间:%@-%@", [Utils getData:responseObject][@"serviceStartTime"], [Utils getData:responseObject][@"serviceEndTime"]];
        }else{
            [self.view Message:responseObject[@"msg"]];
        }
        
    } andFalse:^(NSError *error) {
        [self.view removeAnyView];
    }];
}

- (void)textFieldDidEndEditing:(UITextField *)textField{
    _ProfitLabel.text = [NSString stringWithFormat:@"获得分润点 %.2f", [_MoneyTF.text floatValue]*(_per/100)];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [_MoneyTF resignFirstResponder];
}

- (IBAction)PayClick:(UIButton *)sender {
    LDPushOrderViewController *order = [[LDPushOrderViewController alloc] init];
    order.lineId = self.lineId;
    order.money = _MoneyTF.text;
    [self.navigationController pushViewController:order animated:YES];
}
- (IBAction)QuestionClick:(UIButton *)sender {
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
